package com.gwd.task.controller;

import com.gwd.task.model.AttachmentDTO;
import com.gwd.task.services.AttachmentServer;
import com.gwd.task.services.FileLoadServer;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @author zhiyongming
 * @date 2020/12/14 10:59
 */
@RestController
@RequestMapping(value = "/v1/att")
public class AttachmentController {
    @Resource
    private AttachmentServer attachmentServer;
    @Resource
    private FileLoadServer fileLoadServer;

    /**
     * 文件上传
     * @param file 文件
     * @param tempCode 新增时候用的临时编号
     */
    @PostMapping(value = "/upload")
    public String upload(@RequestParam(value = "file") MultipartFile file,
                         @RequestParam(value = "tempCode") String tempCode) {
        String fileName = fileLoadServer.upLoadFile(file);
        // 保存文件信息到数据库中
        AttachmentDTO dto = new AttachmentDTO();
        dto.setTempCode(tempCode);
        dto.setFileName(fileName);
        dto.setOriginalFilename(file.getOriginalFilename());
        attachmentServer.save(dto);

        return fileName;
    }
}
